package com.hjm.system.service.suser.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hjm.common.core.constants.Constants;
import com.hjm.common.core.domain.suser.LoginUser;
import com.hjm.common.core.domain.Result;
import com.hjm.common.core.enums.ResultCode;
import com.hjm.common.core.enums.UserIdentity;
import com.hjm.redis.service.RedisService;
import com.hjm.security.service.TokenService;
import com.hjm.security.utils.BCryptUtils;
import com.hjm.system.domain.suser.dto.LoginUserDTO;
import com.hjm.system.domain.suser.SysUser;
import com.hjm.common.core.domain.vo.LoginUserVO;
import com.hjm.system.mapper.suser.SysUerMapper;
import com.hjm.system.service.suser.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUerMapper sysUerMapper;

    @Autowired
    RedisService redisService;

    @Autowired
    TokenService tokenService;

    @Value("${jwt.secret}")
     String secret;

    @Override
    public Result<String> login(LoginUserDTO loginUserDTO) {
        if(loginUserDTO.getUserAccount().equals("") || loginUserDTO.getPassword().equals("") ){
            return Result.fail(ResultCode.AILED_USER_EMPTY);
        }
//        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        //              sysUerMapper.selectOne(queryWrapper.select(SysUser::getUserAccount())
        //        对应的mysql 语句：     select * from tb_sys_user where userAccount = {}
/*        SysUser sysUser = sysUerMapper.selectOne(queryWrapper.select(SysUser::getPassword)
                .eq(SysUser::getUserAccount, loginDTO.getUserAccount()));
        // 对应的mysql 语句：  select password from tb_sys_user where userAccount = {}*/
//        new LambdaQueryWrapper<SysUser>().eq()
        SysUser sysUser = sysUerMapper.selectOne(new LambdaQueryWrapper<SysUser>().select(SysUser::getUserId,SysUser::getPassword,SysUser::getNickName).eq(SysUser::getUserAccount, loginUserDTO.getUserAccount()));

//        log.info(sysUser.toString());
/*        LoginResult loginResult = new LoginResult();
        if (sysUser == null) {
            loginResult.setCode(1);
            loginResult.setMsg("用户不存在");
            return loginResult;
//            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(sysUser.getPassword().equals(loginDTO.getPassword())){
            loginResult.setCode(0);
            return loginResult;
//            return Result.ok();
        }
        loginResult.setCode(2);
        loginResult.setMsg("账号或密码错误");
        return loginResult;
//        return Result.fail(ResultCode.FAILED_LOGIN);*/

        if (sysUser == null) {
            return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(!BCryptUtils.matchesPassword(loginUserDTO.getPassword(),sysUser.getPassword())){
            return Result.fail(ResultCode.FAILED_LOGIN);
        }

        // 生成 token 令牌，并发给客户端
        // 后续请求，先进行 token 的验证
        // 延长 token 的时间
        return Result.ok(tokenService.createToken(sysUser.getUserId(), UserIdentity.ADMIN.getValue(),sysUser.getNickName(),null));
    }

    @Override
    public Result<LoginUserVO> info(String token) {
        LoginUser loginUser = tokenService.getLoginUser(token);

        if(loginUser == null){
            return Result.fail();
        }
        LoginUserVO loginUserVO = new LoginUserVO();
        loginUserVO.setNickName(loginUser.getNickName());
        return Result.ok(loginUserVO);
    }

    @Override
    public int addSysUser(LoginUserDTO loginUserDTO) {
        SysUser sysUser = new SysUser();
        sysUser.setUserAccount(loginUserDTO.getUserAccount());
        sysUser.setPassword(BCryptUtils.encryptPassword(loginUserDTO.getPassword()));
        sysUser.setNickName(UserIdentity.ADMIN.getDesc());
        sysUser.setCreateBy(Constants.SYSTEM_USER_ID);

        log.info(sysUser.toString());
        return sysUerMapper.insert(sysUser);
    }

    @Override
    public Boolean exit(String token) {
        return tokenService.deleteLoginUser(token);
    }


}
